package com.ltu.timkor6.gamesite.listings.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.ltu.timkor6.gamesite.shared.CompositeAbstract;
import com.ltu.timkor6.gamesite.shared.Result;

public class ListingsComposite extends CompositeAbstract
{
	private FlexTable table;
	
	public ListingsComposite(Listings listings)
	{
		// Title
		final Label title = new Label("Listings");
		title.setStyleName("panelHeader");
		panel.add(title);
		
		table = new FlexTable();
		table.setStyleName("listings-table");
		table.getRowFormatter().setStyleName(0, "header");
		table.getColumnFormatter().setStyleName(0, "first-column");
		table.getColumnFormatter().setStyleName(1, "second-column");
		panel.add(table);
		
		initWidget(panel);
	}
	
	public void drawLists(List<Result> list)
	{
		int row = 0;
		
		table.setText(row, 0, "Game Title");
		table.setText(row, 1, "Turns");
		table.setText(row, 2, "Points");
		
		for(Result result : list)
		{
			row++;
			
			table.setText(row, 0, result.getGameTitle());
			table.setText(row, 1, String.valueOf(result.getTurns()));
			table.setText(row, 2, String.valueOf(result.getPoints()));
		}
	}
	
	public void drawListsNames(List<Result> list)
	{
		int row = 0;
		
		table.setText(row, 0, "Username");
		table.setText(row, 1, "Points");
		table.setText(row, 2, "Member since");
		
		for(Result result : list)
		{
			row++;
			
			Date date = new Date(((long)result.getDate()) * 1000);
			DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
			
			table.setText(row, 0, result.getUserName());
			table.setText(row, 1, String.valueOf(result.getPoints()));
			table.setText(row, 2, dtf.format(date));
		}
	}
}
